LLM系列 您所在的位置:网站首页 turbo key怎么用 LLM系列

LLM系列

2024-02-07 00:30| 来源: 网络整理| 查看: 265

简介

缺月挂疏桐,漏断人初静。缺月挂疏桐,漏断人初静.jpg

小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖锅盔的小男孩。紧接之前LangChain专题文章:

15:如何用LangChain做长文档问答? 16:如何基于LangChain打造联网版ChatGPT?

今天这篇小作文是LangChain实践专题的第3篇,主要介绍LangChain中OpenAI接口和ChatOpenAI接口的区别,并重点介绍如下内容:

Prompt模板的使用 Chain的使用 Agent的使用 Memory的使用

在介绍上述模块的时候,分别基于OpenAI接口和ChatOpenAI接口以实践的方式演示各个功能模块的使用。

如需完整代码,请在微信公众号:《小窗幽记机器学习》上添加小编微信。

更多、更新文章欢迎关注微信公众号:小窗幽记机器学习。后续会持续整理模型加速、模型部署、模型压缩、LLM、AI艺术等系列专题,敬请关注。

OpenAI 和 ChatOpenAI 的区别

在LangChain中使用OpenAI服务常基于llms模块中的OpenAI API和chat_models中的ChatOpenAI API。

llms模块中的OpenAI接口(langchain.llms.OpenAI)是更通用的接口,用于与不同类型的语言模型进行交互。它可以与各种LLM模型集成,包括不仅限于ChatGPT,还包括其他类型的语言模型,如"text-davinci-003"等。chat_models模块是llms模块接口的高级封装,旨在提供简化的对话式语言模型功能。比如通过langchain.chat_models.ChatOpenAI可以进行多轮对话,并提供方便的方法来处理用户输入和模型输出。

在接口层面,chat_models提供一个以"聊天消息"作为输入和输出的接口。通过将一个或多个消息传递给chat_models,从而可以获取聊天结果。响应也是一条消息。目前LangChain支持的消息类型有AIMessage、HumanMessage、SystemMessage和ChatMessage。其中ChatMessage可以扮演任意角色。在大多数情况下,我们只需要处理HumanMessage、AIMessage和SystemMessage。

总的来说,ChatOpenAI接口更专注于对话式交互,而OpenAI接口更通用,适用于与多种类型的语言模型进行交互和处理不同的语言处理任务。

虽然chat_models模块(langchain.chat_models.ChatOpenAI)和llms模块(langchain.llms.OpenAI)有显著区别,但有时只需将它们视为相同模型。LangChain提供predict接口,使我们可以像与普通LLM(langchain.llms)交互一样与langchain.chat_models模块内的模型进行交互。示例如下:可以对比以下3种方式的输出:

方式1:

from langchain.llms import OpenAI llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0,openai_api_key=openai_api_key,openai_api_base=openai_api_base) res=llm("你是谁") print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

方式2:

from langchain.llms import OpenAI llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0,openai_api_key=openai_api_key,openai_api_base=openai_api_base) res=llm.predict("你是谁") print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

方式3:

from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key,openai_api_base=openai_api_base) res=llm.predict("你是谁") print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

为了方便区分,以下用LLMs表示from langchain.llms import OpenAI接口,用Chat models表示from langchain.chat_models import ChatOpenAI接口。

Prompt模板

大多数LLM应用程序不会将用户输入直接传给LLM。通常情况下,会将用户输入添加到一个称为提示模板(prompt template)的文本片段中。提示模板(prompt template)用以提供关于特定任务的附加上下文。在构建了 Prompt模板后,可以用Chains将定义的模型和Prompt模板串起来。以下先介绍Prompt模板的书写,下一章节展示如何用Chains将模型和Prompt模板串起来。

LLMs版

如果模型选用from langchain.llms import OpenAI, 则可以使用如下的方式构建Prompt模板。

from langchain.prompts import PromptTemplate prompt = PromptTemplate.from_template("你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\ 输出格式为json,key为name和explain。name_msg={person_msg}。") prompt.format(person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈")

此时的prompt内容如下:

'你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。输出格式为json,key为name和explain。name_msg=男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈。' Chat models版

与LLMs,ChatOpenAI接口也可以基于MessagePromptTemplate来使用模板。我们可以基于一个或多个MessagePromptTemplate构建ChatPromptTemplate。在这个过程中可以使用ChatPromptTemplate的format_messages方法来生成格式化的消息。

由于这种方式生成的是消息列表,所以比普通的提示模板稍微复杂一些,普通的提示模板只生成一个字符串。具体示例如下:

from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) template = "你是一名翻译专家,能够将{input_language}翻译成{output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) chat_prompt.format_messages(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?")

此时chat_prompt值如下:

[SystemMessage(content='你是一名翻译专家,能够将中文翻译成英文.', additional_kwargs={}), HumanMessage(content='坤坤会去踩缝纫机吗?', additional_kwargs={}, example=False)] Chain LLMs版

将 OpenAI 接口的模型与PromptTemplate串起来。

from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.llms import OpenAI openai_api_key = "sk-XXX" model_name= "gpt-3.5-turbo" # "text-davinci-003" llm = OpenAI(model_name=model_name, openai_api_key=openai_api_key) prompt = PromptTemplate.from_template("你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\ 输出格式为json,key为name和explain。name_msg={person_msg}?") # prompt.format(person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈") chain = LLMChain(llm=llm, prompt=prompt) person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈" res = chain.run(person_msg) print(res)

输出结果如下:

{ "name": "篮舞花", "explain": "篮舞花是一个形象鲜明的花名,代表着他擅长篮球和舞蹈的特点。穿吊带裤和会打篮球是他的外在特征,而擅长唱歌和跳舞则展示了他的才艺。篮舞花这个花名将他的多重才能融合在一起,形象生动且容易记忆。" } Chat models版

前一章节已经介绍from langchain.chat_models import ChatOpenAI对应的Prompt模板,那么如下用Chain将两者串起来,具体示例如下:

from langchain import LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) chat = ChatOpenAI(temperature=0) # 默认就是 gpt-3.5-turbo template = "你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\ 输出格式为json,key为name和explain。" system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) chain = LLMChain(llm=chat, prompt=chat_prompt) person_msg="男,穿吊带裤,会打篮球,擅长Rap、唱歌和跳舞,特别是篮球类舞蹈" res=chain.run(text=person_msg) print(res)

输出结果如下:

{ "name": "篮舞花", "explain": "取名灵感来源于该人物擅长的篮球类舞蹈。篮舞花既突出了他的篮球特长,又体现了他对舞蹈的热爱和才华。" }

示例2,多个输入:

from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) template = "你是一名翻译专家,能够将{input_language}翻译成{output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) # chat_prompt.format_messages(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?") chat = ChatOpenAI(temperature=0) # 默认就是 gpt-3.5-turbo chain = LLMChain(llm=chat, prompt=chat_prompt) res=chain.run(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?") print(res)

输出结果如下:

Will Kun Kun go and step on a sewing machine? Agents

前文使用的Chain其实是预定义了一序列的步骤。为了处理复杂的工作流程,我们需要能够根据输入动态选择不同的执行任务。LangChain中的代理(Agents)可以做到这一点。它们使用语言模型确定要采取的行动及其顺序。代理可以访问工具,并且它们会重复选择工具、运行工具并观察输出,直到得出最终答案。

要加载一个Agent,我们需要选择以下内容:

设置LLM/Chat模型:这是为Agent提供动力的模型。 工具(Tool):用以执行特定任务。例如:Google搜索、数据库查询、Python REPL等。有关预定义工具可以参阅工具文档。 Agent名称:以字符串设置Agent名称,用于引用支持的Agent类。Agent类在很大程度上由语言模型用于确定要采取的行动的提示参数化。由于本文档重点介绍最简单、最高级别的API,因此仅介绍如何使用标准支持的代理。如果想实现自定义代理,请参见相关文档和custom_agent。

以下,我们将使用SerpAPI来查询搜索引擎。需要安装SerpAPI的Python包:

pip install google-search-results LLMs版

示例代码:

from langchain.agents import AgentType, initialize_agent, load_tools from langchain.llms import OpenAI os.environ["SERPAPI_API_KEY"] = 'XXXX' # The language model we're going to use to control the agent. model_name= "gpt-3.5-turbo" llm = OpenAI(model_name=model_name, temperature=0) # The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in. tools = load_tools(["serpapi", "llm-math"], llm=llm) # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use. agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # Let's test it out! agent.run("现在美元兑换人民币的汇率是多少?该数字除以2的值等于多少?")

日志如下:

> Entering new chain... I need to find the current exchange rate between USD and CNY and then divide it by 2. Action: Search Action Input: "current exchange rate USD CNY" Observation: USDCNY:CUR. USD-CNY X-RATE ; Open. 7.2506 ; Prev Close. 7.2505 ; YTD Return. 5.04% ; Day Range. 7.24237.2523 ; 52 Week Range. 6.69107.3274. Thought:I have found the current exchange rate between USD and CNY. Now I need to divide it by 2. Action: Calculator Action Input: 7.2506 / 2 Observation: Answer: 3.6253 Thought:I now know the final answer. Final Answer: The current exchange rate between USD and CNY is 7.2506, and when divided by 2, it equals 3.6253. > Finished chain.

最终结果如下:

'The current exchange rate between USD and CNY is 7.2506, and when divided by 2, it equals 3.6253.'

人民币汇率.png

虽然结果差不多,但仍有微小的差异,可能是由于该问题答案的时效性导致。

换一个暂时不存在时效性的问题:

agent.run("珠穆朗玛峰多少米?该数字除以2的值等于多少?")

日志如下:

> Entering new chain... I need to find out the height of Mount Everest and then divide it by 2. Action: Search Action Input: "height of Mount Everest" Observation: 29,032′ Thought:I have found the height of Mount Everest, now I need to divide it by 2. Action: Calculator Action Input: 29032 / 2 Observation: Answer: 14516.0 Thought:I now know the final answer. Final Answer: The height of Mount Everest is 29,032 feet and when divided by 2, the value is 14,516 feet. > Finished chain.

输出结果如下:

'The height of Mount Everest is 29,032 feet and when divided by 2, the value is 14,516 feet.' Chat models版

上述相同问题用ChatOpenAI接口的话,可以如下处理:

from langchain.agents import load_tools from langchain.tools.sleep.tool import SleepTool from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI import time # First, let's load the language model we're going to use to control the agent. chat = ChatOpenAI(temperature=0) # Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in. # llm = OpenAI(temperature=0) # 使用这个似乎有问题? tools = load_tools(["serpapi", "llm-math" ], llm=chat) # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use. agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # Now let's test it out! agent.run("珠穆朗玛峰多少米?该数字除以2的值等于多少?") # 结果的单位并不是我们要的"米"

日志如下:

> Entering new chain... Thought: The user is asking for the height of Mount Everest and wants to know the result when it is divided by 2. Action:

{ "action": "Search", "action_input": "height of Mount Everest"}

Observation: 29,032′ Thought:The height of Mount Everest is 29,032 feet. Now I need to divide this number by 2. Action:

{ "action": "Calculator", "action_input": "29032 / 2"}

Observation: Answer: 14516.0 Thought:The height of Mount Everest is 29,032 feet and when divided by 2, the result is 14,516 feet. Final Answer: 14,516 feet. > Finished chain.

结果如下:

'14,516 feet.'

可以看出,输出结果一方面在高度单位上没有符合预期,另一方面第一个问题的回复被遗落。

修改下提问:

agent.run("珠穆朗玛峰多高,单位换算成米?该数字除以2的值等于多少?")

日志如下:

> Entering new chain... Question: How tall is Mount Everest in meters? What is the value when this number is divided by 2? Thought: I can use a search engine to find the height of Mount Everest and then perform the calculation. Action:

{ "action": "Search", "action_input": "height of Mount Everest in meters"}

Observation: 8,848.9 m Thought:The height of Mount Everest is 8,848.9 meters. To find the value when this number is divided by 2, I can use a calculator. Action:

{ "action": "Calculator", "action_input": "8848.9 / 2"}

Observation: Answer: 4424.45 Thought:The height of Mount Everest is 8,848.9 meters. When this number is divided by 2, the value is 4,424.45. Final Answer: The height of Mount Everest is 8,848.9 meters and when divided by 2, the value is 4,424.45. > Finished chain.

结果如下:

'The height of Mount Everest is 8,848.9 meters and when divided by 2, the value is 4,424.45.' 联合Chain和Agent

可以看出,上述Agent的回复都是英文,如果想要使用中文回复呢?可以引入Prompt,将上述Chain的结果再输入到一个用以翻译的Chain。具体实现如下:

from langchain.chains import SimpleSequentialChain # Chain1 - get the answer model_name= "gpt-3.5-turbo" llm = OpenAI(model_name=model_name, temperature=0) # The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in. tools = load_tools(["serpapi", "llm-math"], llm=llm) # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use. agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # Let's test it out! # agent.run("珠穆朗玛峰多少米?该数字除以2的值等于多少?") print("agent.agent.llm_chain.output_keys=",agent.agent.llm_chain.output_keys) chain_one = agent # Chain2 - suggest age-appropriate gift template = """你是一名专业的翻译家,将你收到的内容:{text}翻译成中文,只需要输出最终的翻译结果,不需要输出其他""" prompt_template = PromptTemplate(input_variables=["text"], template=template) chain_two = LLMChain(llm=llm, prompt=prompt_template) overall_chain = SimpleSequentialChain( chains=[chain_one, chain_two], verbose=True) question = "珠穆朗玛峰多高,单位用米?该数字除以2的值等于多少?" overall_chain.run(question)

日志结果如下:

agent.agent.llm_chain.output_keys= ['text'] > Entering new chain... > Entering new chain... I need to find the height of Mount Everest and convert it to meters. Then I need to divide that number by 2. Action: Search Action Input: "height of Mount Everest in meters" Observation: 8,848.9 m Thought:I have the height of Mount Everest in meters. Now I need to divide it by 2. Action: Calculator Action Input: 8848.9 / 2 Observation: Answer: 4424.45 Thought:I now know the final answer. Final Answer: The height of Mount Everest is 8,848.9 meters and when divided by 2, the result is 4,424.45. > Finished chain. The height of Mount Everest is 8,848.9 meters and when divided by 2, the result is 4,424.45. 珠穆朗玛峰的高度为8,848.9米,当除以2时,结果为4,424.45。 > Finished chain.

最终输出结果如下:

'珠穆朗玛峰的高度为8,848.9米,当除以2时,结果为4,424.45。' Memory

上述的Chain和Agent都是无状态的,但对于多数应用程序而言,使用过往的交互信息十分必要,这就需要一个记忆功能。这类记忆上下文信息的能力在聊天机器人等应用中尤为重要,用户希望这些机器人能够记忆过去的上下文从而更好地理解用户新输入的消息。

LangChain中的Memory模块能够用以记忆应用程序的状态。Memory接口很简单,根据最新的运行输入和输出更新状态,同时允许利用存储的状态来修改下一个输入(或者给下一个输入提供上下文)。

LangChain有许多内置的memory系统,其中最简单的是缓存(buffer memory),将最近几个输入/输出添加到当前输入的前面。在下面的示例中,我们基于这种方法进行演示。

LLMs版 from langchain import OpenAI, ConversationChain llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0) conversation = ConversationChain(llm=llm, verbose=True) res=conversation.run("假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞") print(res)

日志输出:

> Entering new chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: 假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞 AI: > Finished chain.

最终输出:

'嗨!你好!我是坤坤,很高兴认识你!我的一技能是Rap,我喜欢用音乐表达自己的情感和想法。我也很喜欢篮球,这是我的二技能。我喜欢在球场上展现我的技巧和团队合作精神。另外,我的三技能是篮球舞。我喜欢跳舞,特别是与篮球相关的舞蹈。这些技能让我感到充满活力和快乐!你有什么问题想问我吗?'

此时再运行如下命令:

res=conversation.run("你好,你叫什么名字?") print(res)

日志输出:

> Entering new chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: 假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞 AI: 嗨!你好!我是坤坤,很高兴认识你!我的一技能是Rap,我喜欢用音乐表达自己的情感和想法。我也很喜欢篮球,这是我的二技能。我喜欢在球场上展现我的技巧和团队合作精神。另外,我的三技能是篮球舞。我喜欢跳舞,特别是与篮球相关的舞蹈。这些技能让我感到充满活力和快乐!你有什么问题想问我吗? Human: 你好,你叫什么名字? AI: > Finished chain.

最终输出:

'嗨!我叫坤坤。很高兴认识你!有什么我可以帮助你的吗?'

从上述结果可以看出,新的对话已经记住历史输入的信息。

Chat models版

我们也可以将Memory与使用ChatOpenAI接口初始化的Chain和Agent一起使用。与LLMs版Memory主要区别在于,不会将所有先前的消息压缩成一个字符串,而是将它们保留为独立的memory对象。

from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template( "假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能(大招)是篮球舞,技能CD都是1秒。开大招的时候,可以沉默敌方所有技能。" ), MessagesPlaceholder(variable_name="history"), HumanMessagePromptTemplate.from_template("{input}") ]) llm = ChatOpenAI(temperature=0) memory = ConversationBufferMemory(return_messages=True) conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm) human_input = "你好啊" res1=conversation.predict(input=human_input) print("human:", human_input) print("bot:",res1) human_input = "你能够简单自我介绍下吗?" res2=conversation.predict(input="human_input") print("human:", human_input) print("bot:",res2)

输出结果如下:

human: 你好啊 bot: 嘿,你好!我是坤坤,有什么我可以帮助你的吗? human: 你能够简单自我介绍下吗? bot: 当然可以!我可以用我的一技能Rap给你表演一段,或者用我的二技能篮球来和你玩一场篮球比赛。如果你想看我最强的技能,我可以使用我的三技能篮球舞,不仅可以展示我的篮球技巧,还可以沉默敌方的技能。你想看哪个技能呢?

此时再执行如下命令:

human_input = "Rap吧" res3=conversation.predict(input="human_input") print("human:", human_input) print("bot:",res3)

输出结果如下:

human: Rap吧 bot: 好的,让我来展示我的一技能Rap吧!请欣赏: Yo,我是坤坤,来自大街小巷, 我的Rap技能,让你们惊叹不已, 我用言语的力量,展现我的才华, 每个字每个句子,都是我的骄傲。 我用节奏和韵律,打造我的旋律, 每个音符都是我心中的力量, 我用文字的力量,表达我的情感, 让你们感受到我的热情和冲动。 我的Rap技能,让你们沉醉其中, 每个字每个音,都是我的真心, 我用音乐的力量,传递我的信息, 让你们跟随我的节奏,一起前行。 这就是我的一技能Rap,希望你喜欢!如果你还想看其他技能的表演,尽管告诉我哦! 总结

本文主要介绍LangChain中OpenAI接口和ChatOpenAI接口的区别,并结合Prompt模板、Chain模块、Agent模块、Memory模块以实践的方式介绍两者的使用差异。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有